﻿<UserControl x:Class="AGMLite.UserControl.ItemsUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:local="clr-namespace:AGMLite.UserControl"             
             mc:Ignorable="d">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:ItemStatusConverter x:Key="ItemStatusConverter" />
                    <local:EntityTypeConverter x:Key="EntityTypeConverter" />
                    <Style TargetType="{x:Type Image}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type UIElement}, AncestorLevel=1}, Path=IsEnabled}"  Value="False">
                                <Setter Property="Opacity"  Value="0.5" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>        
    </UserControl.Resources>
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
        </Grid.RowDefinitions>
            <ToolBar HorizontalAlignment="Stretch" VerticalAlignment="Top" >
                <Button Name="btnAddItem" Command="{Binding AddItemCommand}">
                    <Image Source="/AGMLite;component/images/add.png" Height="20" Width="20" ToolTip="Add" />
                </Button>
                <Button Name="btnEditItem" Command="{Binding EditItemCommand}" CommandParameter="{Binding ElementName=dataGridMonitorItem, Path=SelectedItem}">
                    <Image Source="/AGMLite;component/images/edit.png" Height="20" Width="20"  ToolTip="Edit"/>
                </Button>
                <Button Name="btnDeleteItem" Command="{Binding DeleteItemCommand}"  CommandParameter="{Binding ElementName=dataGridMonitorItem, Path=SelectedItem}">
                    <Image Source="/AGMLite;component/images/delete.png" Height="20" Width="20" ToolTip="Delete" />
                </Button>
                <Button Name="btnSyncItem" Command="{Binding SyncItemCommand}"  CommandParameter="{Binding ElementName=dataGridMonitorItem, Path=SelectedItem}">
                    <Image Source="/AGMLite;component/images/sync.png" Height="20" Width="20" ToolTip="Sync" />
                </Button>
                <Button Name="btnViewItem" Command="{Binding ViewEntityListCommand}"  CommandParameter="{Binding ElementName=dataGridMonitorItem, Path=SelectedItem}">
                    <Image Source="/AGMLite;component/images/view.png" Height="20" Width="20" ToolTip="View" />
                </Button>
            </ToolBar>
            <DataGrid Grid.Row="1" AutoGenerateColumns="False" 
                          HorizontalAlignment="Stretch" 
                          Name="dataGridMonitorItem" 
                          VerticalAlignment="Stretch" 
                          SelectionMode="Single" 
                          ItemsSource="{Binding Project.ItemCollection}"
                          RowHeaderWidth="0" 
                          ColumnHeaderHeight="0"  
                          CanUserResizeColumns="False" 
                          CanUserSortColumns="False" 
                          CanUserReorderColumns="False" 
                          GridLinesVisibility="None" 
                          CanUserAddRows="False"
                          AllowDrop="True"
                      EnableRowVirtualization="True"
                      ScrollViewer.IsDeferredScrollingEnabled="True">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <i:InvokeCommandAction Command="{Binding ViewEntityListCommand}"  CommandParameter="{Binding SelectedItem, ElementName=dataGridMonitorItem}"  />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <DataGrid.InputBindings>
                        <KeyBinding Key="DELETE" Command="{Binding DeleteItemCommand}" CommandParameter="{Binding SelectedItem, ElementName=dataGridMonitorItem}" />
                    </DataGrid.InputBindings>
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Sync" Command="{Binding SyncItemCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}">
                                <MenuItem.Icon>
                                    <Image Source="/AGMLite;component/images/sync.png"  Height="20" Width="20"/>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="View" Command="{Binding ViewEntityListCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}">
                                <MenuItem.Icon>
                                    <Image Source="/AGMLite;component/images/view.png"  Height="20" Width="20"/>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Edit" Command="{Binding EditItemCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}">
                                <MenuItem.Icon>
                                    <Image Source="/AGMLite;component/images/edit.png"  Height="20" Width="20"/>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Delete" Command="{Binding DeleteItemCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}">
                                <MenuItem.Icon>
                                    <Image Source="/AGMLite;component/images/delete.png" Height="20" Width="20" />
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Width="40" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Image Height="16" Width="16" Source="{Binding Status, Converter={StaticResource ItemStatusConverter}}" VerticalAlignment="Center" ToolTip="{Binding Message}" Margin="2">
                                            <Image.Style>
                                                <Style>
                                                    <Style.Triggers>
                                                        <EventTrigger RoutedEvent="Image.Loaded">
                                                            <BeginStoryboard>
                                                                <Storyboard>
                                                                    <DoubleAnimation Storyboard.TargetProperty="(Image.Opacity)" BeginTime="0:0:0" Duration="0:0:0.5" From="1.0" To="0.0" RepeatBehavior="Forever" AutoReverse="True"/>
                                                                </Storyboard>
                                                            </BeginStoryboard>
                                                        </EventTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Image.Style>
                                        </Image>
                                        <Image Height="16" Width="16" Source="{Binding EntityType, Converter={StaticResource EntityTypeConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center" ToolTip="{Binding EntityType}" />
                                    </StackPanel>                                    
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>                        
                        <DataGridTemplateColumn Width="*" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" Height="24" Margin="1">
                                        <TextBlock Margin="1"  VerticalAlignment="Center">
                                                <TextBlock.Text>
                                                    <MultiBinding StringFormat="{}({1}) {0}">
                                                        <Binding Path="Name" />
                                                        <Binding Path="Total" />
                                                    </MultiBinding>
                                                </TextBlock.Text>
                                            <TextBlock.Style>
                                                <Style TargetType="TextBlock">
                                                    <Style.Setters>
                                                        <Setter Property= "FontWeight" Value="Bold"/>
                                                        <Setter Property= "FontSize" Value="12"/>
                                                    </Style.Setters>
                                                </Style>
                                            </TextBlock.Style>
                                        </TextBlock>
                                    </StackPanel>                                    
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
            <StatusBar Grid.Row="2"  HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
                <TextBlock Text="{Binding Project.Message}" HorizontalAlignment="Stretch"/>
            </StatusBar>
    </Grid>
</UserControl>
